/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.wind.hazard;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.hazards.wind.WindConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.WindHazard;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;

public class WindHazardForm extends AbstractForm implements BuildingUpdate {

	private Label exposure;
	private Label topography;
	private Label shielding;
	private Label shieldingHeight;
	private Label debrisSource;
	
	// form fields
	private WindExposureListBox formExposure = new WindExposureListBox();
	private WindTopographyListBox formTopography = new WindTopographyListBox();
	private WindShieldingListBox formShielding = new WindShieldingListBox();
	private WindShieldingHeightListBox formShieldingHeight = new WindShieldingHeightListBox();
	private WindbornDebrisSourceListBox formDebrisSource = new WindbornDebrisSourceListBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(5,2);
	
	// entities
	private WindHazard windHazard;
	
	// listeners
	private WindHazardListBoxChangeListener windHazardListBoxChangeListener;
	private WindConstants constants;

	public WindHazardForm(Building building) {

		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (WindConstants)GWT.create(WindConstants.class);
		tabTitle = constants.windHazardTabTitle();
		
		exposure = new Label(constants.windHazardExposure());
		topography = new Label(constants.windHazardTopography());
		shielding = new Label(constants.windHazardShielding());
		shieldingHeight = new Label(constants.windHazardShieldingHeight());
		debrisSource = new Label(constants.windHazardDebrisSource());
		
		// add labels to form
		table.setWidget(0, 0, exposure);
		table.setWidget(1, 0, topography);
		table.setWidget(2, 0, shielding);
		table.setWidget(3, 0, shieldingHeight);
		table.setWidget(4, 0, debrisSource);
		
		// add fields to table
		windHazard = building.getWindHazard();
		windHazardListBoxChangeListener = new WindHazardListBoxChangeListener(windHazard);
		
		// listeners
		loadListeners();
		
		// forms
		loadForms();
		
		// add fields to table
		table.setWidget(0, 1, formExposure);
		table.setWidget(1, 1, formTopography);
		table.setWidget(2, 1, formShielding);
		table.setWidget(3, 1, formShieldingHeight);
		table.setWidget(4, 1, formDebrisSource);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);			


	}

	public void updateBuilding(Building building) {

		// entities
		setBuilding(building);
		this.windHazard = building.getWindHazard();
		
		// listeners
		windHazardListBoxChangeListener.updateWindHazard(windHazard);
		
		// reload
		loadForms();
		
	}
	
	private void loadForms()
	{
		
		if( windHazard != null)
		{
			formExposure.setListItem(windHazard.getExposure());
			formExposure.setName(WindHazard.EXPOSURE);
			
			formTopography.setListItem(windHazard.getTopography());
			formTopography.setName(WindHazard.TOPOGRAPHY);
			
			formShielding.setListItem(windHazard.getShielding());
			formShielding.setName(WindHazard.SHIELDING);
			
			formShieldingHeight.setListItem(windHazard.getShieldingHeight());
			formShieldingHeight.setName(WindHazard.SHIELDINGHEIGHT);
			
			formDebrisSource.setListItem(windHazard.getDebrisSource());
			formDebrisSource.setName(WindHazard.DEBRISSOURCE);
		}
		
		if( building != null)
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formExposure.setEnabled(false);
	            formTopography.setEnabled(false);
	            formShielding.setEnabled(false);
	            formShieldingHeight.setEnabled(false);
	            formDebrisSource.setEnabled(false);
	        }
	        else
	        {
	            formExposure.setEnabled(true);
	            formTopography.setEnabled(true);
	            formShielding.setEnabled(true);
	            formShieldingHeight.setEnabled(true);
	            formDebrisSource.setEnabled(true);            
	        }
		}
	}
	
	private void loadListeners()
	{
		formExposure.addChangeListener(windHazardListBoxChangeListener);
		formTopography.addChangeListener(windHazardListBoxChangeListener);
		formShielding.addChangeListener(windHazardListBoxChangeListener);
		formShieldingHeight.addChangeListener(windHazardListBoxChangeListener);
		formDebrisSource.addChangeListener(windHazardListBoxChangeListener);
		
	}

}
